import { shareVerifyTicket } from '@/api/scene';
import { useUserStore } from '@jl/common/store';
import router from '.';
import { useGlobalStore } from '@/store/global';
const whiteList = new Set<string>([
  'scenePreviewShare',
  'scenePreviewShareValidate',
  'shareNoAuth',
]);

export const applyPermissionGuard = () => {
  router.beforeEach(async (to: any, from, next) => {
    // 在白名单中, 放行
    if (whiteList.has(to.name)) {
      if (to.name === 'scenePreviewShare') {
        // const localShareCode = localStorage.getItem('shareCode');
        // const jlUrlShareTicket = Cookies.get('jlUrlShareTicket');
        if (to.params.shareCode) {
          const verifyTicketResp = await shareVerifyTicket({
            data: { recordAccess: 0, shareCode: to.params.shareCode },
          });
        }
      }
      next();
      return;
    }
    const userStore = useUserStore();
    const globalStore = useGlobalStore();
    const sso = await userStore.sso(process.env.VUE_APP_ID!);
    if (sso) {
      globalStore.getAppBaseConfig(process.env.VUE_APP_ID!);
      globalStore.getCompany();
      next();
    }
  });
};
